/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfaceFazerLogin.java
 *
 * Created on 23/06/2010, 19:57:49
 */
package gui;

import javax.swing.JOptionPane;

import controller.BolaoFacade;
import controller.Sessao;


import exception.ExceptionLogin;
import exception.ExceptionSenha;

/**
 * Interface Login
 * @author Claudio Cardoso, Daniel Leite, Lucas Sampaio, Matheus Sampaio
 * @version 1.0
 */
public class InterfaceFazerLogin extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BolaoFacade bolaoFacade = new BolaoFacade();

    /** Creates new form InterfaceFazerLogin */
    public InterfaceFazerLogin() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        emailLogin = new java.awt.Label();
        senhaLogin = new java.awt.Label();
        textEmailFazerLogin = new javax.swing.JTextField();
        entrarInterfaceUsuario = new javax.swing.JButton();
        voltarInterfaceInicial = new javax.swing.JButton();
        textSenhaLogin = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Bolão da Copa P2 - Login");

        emailLogin.setText("Email: ");

        senhaLogin.setText("Senha:");

        entrarInterfaceUsuario.setText("Entrar");
        entrarInterfaceUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entrarInterfaceUsuarioActionPerformed(evt);
            }
        });

        voltarInterfaceInicial.setText("Voltar");
        voltarInterfaceInicial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarInterfaceInicialActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(senhaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textEmailFazerLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                    .addComponent(textSenhaLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(voltarInterfaceInicial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)
                .addComponent(entrarInterfaceUsuario)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(emailLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textEmailFazerLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(senhaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textSenhaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entrarInterfaceUsuario)
                    .addComponent(voltarInterfaceInicial))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void voltarInterfaceInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voltarInterfaceInicialActionPerformed
        new InterfaceInicial().setVisible(true);
        dispose();
    }//GEN-LAST:event_voltarInterfaceInicialActionPerformed

    @SuppressWarnings("deprecation")
	private void entrarInterfaceUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_entrarInterfaceUsuarioActionPerformed
        String senha = textSenhaLogin.getText();
        String email = textEmailFazerLogin.getText();

        try {

        	if (bolaoFacade.pesquisaJogador(email) == null)
        		throw new ExceptionLogin("Usuário não cadastrado.");

        	if (!bolaoFacade.pesquisaJogador(email).getSenha().equals(senha))
        		throw new ExceptionSenha("Senha errada.");

        	Sessao.getInstance().setJogadorLogado(bolaoFacade.pesquisaJogador(email));
        } catch (ExceptionSenha e) {
        	JOptionPane.showMessageDialog(this, e.getMessage());
        	return;
        } catch (ExceptionLogin e) {
        	JOptionPane.showMessageDialog(this, e.getMessage());
        	return;
        }

        if (Sessao.getInstance().getJogadorLogado().getEmail().equals("root")) {
        	new InterfaceRoot().setVisible(true);
        	dispose();
        } else {
        	new InterfaceUsuario().setVisible(true);
        	dispose();
        }
    }//GEN-LAST:event_entrarInterfaceUsuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfaceFazerLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Label emailLogin;
    private javax.swing.JButton entrarInterfaceUsuario;
    private java.awt.Label senhaLogin;
    private javax.swing.JTextField textEmailFazerLogin;
    private javax.swing.JPasswordField textSenhaLogin;
    private javax.swing.JButton voltarInterfaceInicial;
    // End of variables declaration//GEN-END:variables
}
